#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    :   test.py    
@Contact :   gaojingyu@lyzdfintech.com

@Modify Time           @Author      @Version      @Desciption
-----------------    ----------    ----------    ------------
2021/4/7 5:59 下午     gaojingyu       1.0           None
"""
# -*- coding: utf-8 -*-
import json

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from backend.models import Lyzd_Mock_Config
from backend.serializers import Lyzd_Mock_ConfigSerializer


@csrf_exempt
def mock_api(request):
    try:
        print(request
              )
        print(type(request.path))
        print(request.path)
        mockAPI = request.path.replace("/mock_api/", '')
        print(mockAPI)

        """
          obi = Lyzd_Interface_Case.objects.filter(interface_id__id=interfaceID,
                                                                 case_name__contains=case_name, project=projectID,
                                                                 case_type__in=(0, 1)).order_by(
                            "delete_flag", "-id")
        """

        obj = Lyzd_Mock_Config.objects.filter(interface_name_en=mockAPI)
        print(obj)
        serialize = Lyzd_Mock_ConfigSerializer(obj, many=True)
        print(serialize.data[0]['mock_outparam'])
        output = serialize.data[0]['mock_outparam']
        print(type(output))

        # aaaaa = {
        #     "timestamp": "2021-04-15T07:55:17.186+0000",
        #     "status": 404,
        #     "error": "Not Found",
        #     "message": "No message available",
        #     "path": "/kpHAR3d07syIwiNfRB3j5sX3CghyhZN3COgaEhpqHBBD5r6h49OHjDkdKpJGaPwx146QfYsb+lCegCc6HnJs70/8zFmjDCNyzJOyALTgav5eeogs8ZY1vXB7DdHg51XRkcgjnP9t8nesyaJBhPEEtEgQZk6DHmEQ80d1wGL6Lk/r9ebB+zlubIAZwFw4any6w1BPIjN/F8w/QtKEkg/q8LjQZLQZJy6MeS6IVidcA3xqX/nq1sdIlL8bmXfVAK5lrlk1ymB0y15G+fAZN2Hhe3snCagmsazjxXHWTrBJNZ5m9cexFioqdBMOPinHjnAURtojC15pD49xgP6/HjevDeT8wL7yDq54YL271LMSumL90aesw9lxFgC/ECk5olanqr9A+Npif0H5sVrpHEeMQ/AfcmYumcua2z5K/neIR4HilYnGRR7oLEmBGltkXbrZ42ACX+/tcaP1qtSocr7nZ5WQV5VhG8m/rHFXN80QSEy2oqVXWn7rlAWcX1xAEIL6W1xoNWcqKugIbxIG27NDYev6ZPuvVby+DJf5PcE9Zjhbs+RApuTy9r4kFZIbVqEmnFrJUmTkwvP4oNMfJfr4cfhbMWnCb2NLmYPGCSUB6ayLOKTOI19Sl3jDTYuRwSZ6Wh+Ar6GWimlRKrwae+FyUvjswk/dMB1a2p1DFHtst+0HUKVSaz9fXvygXmuBn2GHZTCTgcCshIJ6s1UR1a+tBywiFZFmwjRCGzVglZ0HSw5NiBNn1aeUeOQb/Afm4daMfdSWszkEfRTvOCfElgy6yoenb8HrL1rwaCfHLHIrWS9M5TzbiYoTFVxc/g1atbMUojyN9Mf9tqWA8lVZr1ILtZeHYDHiaXTg9b5bkee3HwhkUiY5eSnU8H/KQJOnkafr1dE+ZGZ4loLvJKQ+fXkzMmS3L/IUtVuDN0/ROytQnd/zuNVMgeCznYSK1YmAvM2hRWEHSJA8CD6hugqvMrFXys1kNhDHQvC/aT2PZLXUQzL7HuEzVxv7p+3OBB6Szqka5EWhrpmK1aQoKxZv7z9LjiH/IghObNHBYABGZRrthzT6e5/cr9T0lej87zWTGT3+zQ6IetNugF7mDDnZs4fhgG9VE+sAD5lJ2eyChTHGkd9kQp4XsJtECDKJN3Tk8CqD4bzWOfRTlMcptHNbLpdKPySPXmR44zzBrKL5/it/TpGh95Q0JFCsNRLnlsdxwbjrgcujw0RDavjj4Dab1n+GxwKsfB5YfEOMm1tb+cdgp2KW6LannyalSBrwxK2A85g5CMe4qmPp3U4JR2SJOdTC5vUrDkHvN78pzLT7Ue2w+r8H9Ixl36wfuHfmKttPx4oOQRq9PhKkq9hfcC50IsQ+mwexLr9JhJGGvnEHp4BFJhBtC9z+WaeMliSwAhjwE1uyCa/xiuvS0KctLRIV4CrrjKzDNiohBM5jesfoqbJ2gUV31"
        # }
        json_output = json.loads(output)
        print(json_output)
        print(1)
        # data = {"code": "200", "message": "success", "data": output}
        return JsonResponse(data = {"code": "200", "message": "success", "out_put_data": json_output},
                            safe=False)

    except:
        data = {
            "code": "404",
            "message": "失败",
            "failure reason": "不存在的mock接口名"
        }
        print(type(data))
        json_output = json.dumps(data)
        print(json_output)
        return JsonResponse( data = {
            "code": "404",
            "message": "失败",
            "failure reason": "不存在的mock接口名"
        })

    # return JsonResponse(data={"data": serialize.data,
    #                           "page": page,
    #                           "total": total
    #                           }, code="999999", msg="成功")
    # msg_data = Lyzd_RunMsg.objects.filter(id=maxid_msg_data.id)
    #
    # serialize = Lyzd_RunMsgSerializer(msg_data, many=True)

    # result_str = (serialize.data[0]['msg'])
